/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

package org.solmix.fmk.i18n;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.NoSuchElementException;

import org.solmix.commons.util.SimpleFilter;

/**
 * 
 * @author Administrator
 * @version $Id$ 2011-3-15
 */

@SuppressWarnings("unchecked")
public class FilterEnumeration implements Enumeration
{

   private final URL k_path;

   private final List<String> k_filePattern;

   private boolean k_over = false;

   private int k_file_idx = 0;

   private final List<Object> k_nextEntries = new ArrayList<Object>(2);

   public FilterEnumeration(String path, String filePattern, boolean recurse)
   {
      if (path == null)
      {
         throw new IllegalArgumentException("the path of filter entries cannot be null");
      }
      if (path.length() > 0 && (path.toUpperCase().startsWith("CLASSPATH:")))
      {
         path = path.substring(10);
         if ( !path.startsWith( "/" ) )
            path = "/" + path;
      }
      k_path = getClass().getResource( path );
      filePattern = (filePattern == null) ? "*" : filePattern;
      k_filePattern = SimpleFilter.parseSubstring(filePattern);
      findNext();
   }
   /**
    * 
    */
   private void findNext()
   {
      File file = null;
      try
      {
         file = new File( k_path.toURI() );
      } catch ( URISyntaxException e1 )
      {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
      if (!k_over)
      {
         try
         {
            if ( file != null && file.exists() )
               if (!file.isDirectory())
               {
                  k_over = true;

                  k_nextEntries.add(file.toURI().toURL());

               } else
               {
                  File[] files = file.listFiles();
                  while (files != null && k_file_idx < files.length && k_nextEntries.size() == 0)
                  {
                        if (SimpleFilter.compareSubstring(k_filePattern, files[k_file_idx].getName()))
                        {
                           k_nextEntries.add(files[k_file_idx].toURI().toURL());
                        }
                        k_file_idx++;
                  }
               }
         } catch (MalformedURLException e)
         {
            // igonore.
         }
      }

   }

   /**
    * {@inheritDoc}
    * 
    * @see java.util.Enumeration#hasMoreElements()
    */
   @Override
   public boolean hasMoreElements()
   {
      return (k_nextEntries.size() != 0);
   }

   /**
    * {@inheritDoc}
    * 
    * @see java.util.Enumeration#nextElement()
    */
   @Override
   public Object nextElement()
   {
      if (k_nextEntries.size() == 0)
      {
         throw new NoSuchElementException("No more entries.");
      }
      Object last = k_nextEntries.remove(0);
      findNext();
      return last;
   }

}
